package com.example.userclient

import android.util.Log
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.utils.DistanceUtil
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.adapter.NavigationAdapter


//http://lbs.baidu.com/index.php?title=androidsdk/guide/create-project/androidstudio
//http://lbsyun.baidu.com/apiconsole/key#/home
//http://lbsyun.baidu.com/index.php?title=sdk/download&action#selected=mapsdk_basicmap,mapsdk_searchfunction,mapsdk_lbscloudsearch,mapsdk_calculationtool,mapsdk_radar
class MyLocationListener() : BDAbstractLocationListener() {
    override fun onReceiveLocation(location: BDLocation?) {


        //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
        //以下只列举部分获取经纬度相关（常用）的结果信息
        //更多结果信息获取说明，请参照类参考中BDLocation类中的说明
        val latitude: Double = location?.latitude ?: 0.0 //获取纬度信息
        val longitude : Double = location?.longitude  ?: 0.0 //获取纬度信息
        val radius = location!!.radius
        val coorType = location.coorType
        //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准

        //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准
        val errorCode = location.locType
//        Log.i(
//            "MyLocationListener",
//            "latitude：$latitude ,longitude：$longitude ,radius：$radius ,coorType：$coorType ,errorCode：$errorCode"
//        )

        val position=  Position(latitude, longitude, coorType,location.addrStr)
        ClientInfoHolder.position = position
        Log.i(
            "MyLocationListener",
            "latitude：${ClientInfoHolder.position!!.latitude} ,longitude：${ClientInfoHolder.position!!.longitude} ,radius：$radius ,coorType：$coorType ,errorCode：$errorCode"
        )
        val locationDescribe = location.locationDescribe //获取位置描述信息
        Log.i("MyLocationListener", "结果" + locationDescribe)
        ClientInfoHolder.locationDescribe = locationDescribe

        val locData = MyLocationData.Builder()
            .accuracy(location.radius) // 此处设置开发者获取到的方向信息，顺时针0-360
            .direction(location.direction).latitude(location.latitude)
            .longitude(location.longitude).build()

        ClientInfoHolder.myloc = locData
        ClientInfoHolder.ll = LatLng(latitude,longitude)

        val list  = ArrayList<HeartBeatInfo>()
        ClientInfoHolder.clientMap?.let {
            for( i in ClientInfoHolder.clientMap!!.values){
                val distance = DistanceUtil.getDistance(ClientInfoHolder.ll,
                        LatLng(i.position.latitude,i.position.longitude)
                )
                if (distance<1000){
                    list.add(i)
                }
            }
        }
        ClientInfoHolder.clientMapWithin1000 = list
        NavigationAdapter.dataSet = list
        Log.i("clientMapWithin1000","list Size : ${list.size}")


        ClientInfoHolder.notifyChanged()
        Log.i("MyLocationListener","${locData.latitude}-${locData.longitude}-${locData.direction}")
    }
}